// 04jyhs/setting/accountNumber/accountNumber.js
var co = require("../../../resource/js/common.js")
var M = require("../../../resource/js/md5.js")
var app = getApp()
var _this,_data,count = 0
Page({
   /*页面的初始数据*/
   data: {
      shopPassword: '', //商家是否设置过密码      
      // 账户信息
      account: '',
      // 显示修改登录密码
      isShow: 0,
      oldInfo: 0,
      newInfo: 0,
      // 显示修改支付密码
      isPayShow: 0,
      oldPayInfo: 0,
      newPayInfo: 0,
      payHint: false,
      extract_input: 0,//提成率
      bizID:0,
   },

   /*生命周期函数--监听页面加载 */
   onLoad: function () {
      _this = this
      _data=_this.data
      console.log(app.Data)
      var Password = app.Data.shopInfo.PayPassword_Is_Set ? app.Data.shopInfo.PayPassword_Is_Set : app.Data.userInfo.PayPassword_Is_Set
      this.setData({
         account: app.Data.userInfo,
         bizID: app.Data.shopInfo.Biz_ID,
         shopPassword: Password,
         extract_input: app.Data.shopInfo.Biz_Fee_Value * 100 
      })
   },
   // 显示登录密码修改框
   showChange: function () {
      var isShow = this.data.isShow == 0 ? 1 : 0;
      this.setData({
         isShow: isShow
      })
   },
   //  显示支付密码修改框
   payShow: function () {
      var isPayShow = _this.data.isPayShow == 0 ? 1 : 0;
      this.setData({
         isPayShow: isPayShow,
         payHint: false
      })
   },
   // 修改密码
   changePassWord: function (e) {
      console.log("【获取e】===", e.detail.value)
      var oldPassWord = e.detail.value.oldPassWord
      var newPassWord = e.detail.value.newPassWord
      var oldInfo, newInfo
      if (oldPassWord == '' && newPassWord == '') {
         oldInfo = 1
         newInfo = 1
      } else if (oldPassWord == '') {
         oldInfo = 1
      } else if (newPassWord == '') {
         newInfo = 1
      } else {
         co.getShareApi('jyhs/Usermg/updateInfo', {
            User_ID: app.Data.userInfo.User_ID,
            User_Password: M.md5(oldPassWord),
            User_Password_New: M.md5(newPassWord)
         }, function (res) {
            console.log('res===', res)
            if (res.code == 0) {
               co.toastAllText(res.msg)
               _this.setData({
                  isShow: 0
               })
            } else {
               co.Toast(res.msg)
            }
         })
      }
      this.setData({
         oldInfo: oldInfo,
         newInfo: newInfo
      })
   },
   //  修改支付密码
   payPassword: function (e) {  
      console.log("dada",app.Data)
      var oldPayInfo = 0,
         newPayInfo = 0,
         Biz_New_PayPassword = e.detail.value.Biz_New_PayPassword,
         Biz_PayPassword = e.detail.value.Biz_PayPassword
      if (_this.data.shopPassword == 0) {//第一次设置密码
         if (co.empty(app.Data.shopInfo.Biz_ID)){//设置回收帮人的支付密码
            var param = {
               User_ID: app.Data.userInfo.User_ID,
               User_PayPassword: M.md5(""),
               User_PayPassword_New: M.md5(Biz_New_PayPassword)
            }
         }else{//设置91支付密码
            var param = {
               Biz_New_PayPassword: M.md5(Biz_New_PayPassword),
               Biz_ID: app.Data.shopInfo.Biz_ID
            }
         }
      } else {//第二次密码设置
         if (co.empty(Biz_PayPassword)) {
            oldPayInfo = 1
         }
         if (co.empty(app.Data.shopInfo.Biz_ID)) {
            var param = {
               User_ID: app.Data.userInfo.User_ID,
               User_PayPassword: M.md5(Biz_PayPassword),
               User_PayPassword_New: M.md5(Biz_New_PayPassword)
            }
         }else{
            var param = {
               Biz_PayPassword: M.md5(Biz_PayPassword),
               Biz_New_PayPassword: M.md5(Biz_New_PayPassword),
               Biz_ID: app.Data.shopInfo.Biz_ID,
            }
         }
      }
      if (co.empty(Biz_New_PayPassword)) {
         newPayInfo = 1
      }
      if (Biz_New_PayPassword.length < 6) {
         newPayInfo = 1;
         this.setData({
            payHint: '支付密码不小于6位数',
            newPayInfo: newPayInfo
         })
         return
      }
      if (oldPayInfo == 1 || newPayInfo == 1) {
         _this.setData({
            oldPayInfo: oldPayInfo,
            newPayInfo: newPayInfo
         })
         return
      }
      if (co.empty(app.Data.shopInfo.Biz_ID)) {
         _this.setJyhsbPassword(param)
      }else{
         _this.setJyhsbAdminPassword(param)
      }
   },
   //修改回收帮人员支付密码
   setJyhsbPassword(param) {
      co.getShareApi("jyhs/Usermg/updateInfo",param,function(res){
         if (res.code == 0) {
            co.toastAllText(res.msg)
            _this.setData({
               isPayShow: 0,
               payHint: false,
               shopPassword: 1
            })
            app.Data.userInfo.PayPassword_Is_Set = 1
            _this.setData({
               payHint: res.msg
            })
         } else {
            co.toastAllText(res.msg)
         }
      })
   },
   //调用修改支付密码接口
   setJyhsbAdminPassword(param) {
      co.getShareApi("jyhs/Admin/setPassword", param, function (res) {
         if (res.code == 0) {
            co.toastAllText(res.msg)
            _this.setData({
               isPayShow: 0,
               payHint: false,
               shopPassword: 1
            })
            app.Data.shopInfo.PayPassword_Is_Set = 1
            wx.setStorageSync('shopInfo', app.Data.userInfo)
            _this.setData({
               payHint: res.msg
            })
         } else {
            co.Toast(res.msg)
         }
      })
   },
   // 关闭提示
   closeInfo: function () {
      this.setData({
         oldInfo: 0,
         newInfo: 0,
         oldPayInfo: 0,
         newPayInfo: 0
      })
   },
   //跳转到忘记支付密码
   retrievePwd: function () {
      wx.navigateTo({
         url: '/03admin/04setting/00account/retrievePassword/retrievePassword'
      })
   },
   //获取提成率的输入值
   extract_input(e) {
      _data.extract_input = e.detail.value / 100
      console.log(_data.extract_input)
   },
   //设置提成
   save_extract() {
      co.getShareApi("jyhs/Admin/saveInfo",{
         Biz_ID: 91,
         Biz_Fee_Value: _data.extract_input
      },function(res){
         if(res.code == 0){
            _this.setData({
               extract_input: _data.extract_input * 100
            })
            co.toastAllText(res.msg)
         }
      })
   },
})